package com.restaurant.application.indexes.accumulategoodslist.mysql;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.nebutown.base.Unit;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.nebutown.supporthome.AbstractIndex;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

public class AccumulateGoodsListImpl extends AbstractIndex<AccumulateGoodsListCriteria, AccumulateGoodsListBean> {

    @Override
    public String getTableName() {
        return "" + getConnectionName() + ".ACCUMULATEGOODSLIST";
    }

    @Override
    public AccumulateGoodsListBean newBean() {
        return new AccumulateGoodsListBean();
    }

    @Override
    public AccumulateGoodsListCriteria newCriteria() {
        return new AccumulateGoodsListCriteria();
    }

    @Override
    public List<String> getFieldNames() {
        return Lists.newArrayList(
                "goodsUID",
                "goodsName",
                "state",
                "goodsType",
                "accumulatePoints",
                "goodsNumber",
                "soldNumber",
                "goodsId",
                "createTime",
                "modifyTime",
                "goodsDescribe",
                "shopId",
                "typeId",
                "price");
    }

    @Override
    public List<String> getDateFields() {
        return Lists.newArrayList(
                "accumulatePoints",
                "goodsNumber",
                "soldNumber",
                "createTime",
                "modifyTime");
    }

    @Override
    public List<String> getDoubleFields() {
        return Lists.newArrayList(
                "price");
    }

    @Override
    public Map<String, String> getFieldLabels() {
        return ImmutableMap.<String, String>builder()
                .put("goodsName", "商品名")
                .put("state", "商品状态")
                .put("goodsType", "商品类别")
                .put("accumulatePoints", "所需积分")
                .put("goodsNumber", "商品数量")
                .put("soldNumber", "售出数量")
                .put("goodsId", "商品Id")
                .put("createTime", "创建时间")
                .put("modifyTime", "修改时间")
                .put("goodsDescribe", "商品描述")
                .put("shopId", "店铺Id")
                .put("typeId", "种类Id")
                .put("price", "商品价格")
                .build();
    }

    @Override
    public List<String> getContributors() {
        return Lists.newArrayList(
                "com.restaurant.application.genshanguser.GenShangUser");
    }

    @Override
    public AccumulateGoodsListBean fakeBean() {
        AccumulateGoodsListBean bean = new AccumulateGoodsListBean();

        bean.setGoodsUID(UID.recover("A 1604913647065144860 looooong string"));
        bean.setGoodsName("A -3857162669443701610 looooong string");
        bean.setState("A 4273688036075767602 looooong string");
        bean.setGoodsType("A -8366546743646346886 looooong string");
        bean.setAccumulatePoints(-88144253L);
        bean.setGoodsNumber(39157964L);
        bean.setSoldNumber(-1070422151L);
        bean.setGoodsId("A 2842355300548544772 looooong string");
        bean.setCreateTime(-149942810L);
        bean.setModifyTime(-2131398727L);
        bean.setGoodsDescribe("A 5297161161666339501 looooong string");
        bean.setShopId("A -6694727404279961870 looooong string");
        bean.setTypeId("A -2163838660786579989 looooong string");
        bean.setPrice(0.668132095418693);
        return bean;
    }

    @Override
    public String getCreateTableStatement() {
        StringBuilder buf = new StringBuilder();

        buf.append("CREATE TABLE IF NOT EXISTS " + getConnectionName() + ".ACCUMULATEGOODSLIST (\n");
        buf.append("    RECORD_ID BIGINT AUTO_INCREMENT,\n");
        buf.append("    GOODSUID CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    GOODSNAME VARCHAR(255),\n");
        buf.append("    STATE VARCHAR(255),\n");
        buf.append("    GOODSTYPE VARCHAR(255),\n");
        buf.append("    ACCUMULATEPOINTS BIGINT,\n");
        buf.append("    GOODSNUMBER BIGINT,\n");
        buf.append("    SOLDNUMBER BIGINT,\n");
        buf.append("    GOODSID VARCHAR(255),\n");
        buf.append("    CREATETIME BIGINT,\n");
        buf.append("    MODIFYTIME BIGINT,\n");
        buf.append("    GOODSDESCRIBE VARCHAR(255),\n");
        buf.append("    SHOPID VARCHAR(255),\n");
        buf.append("    TYPEID VARCHAR(255),\n");
        buf.append("    PRICE REAL,\n");
        buf.append("PRIMARY KEY (RECORD_ID)) CHARACTER SET  = utf8mb4 COLLATE = utf8mb4_bin");
        return buf.toString();
    }

    @Override
    public String getInsertDataStatement(AccumulateGoodsListBean data) {
        StringBuilder buf = new StringBuilder();

        buf.append("INSERT INTO " + getConnectionName() + ".ACCUMULATEGOODSLIST (\n    ");
        buf.append("    GOODSUID, GOODSNAME, STATE, GOODSTYPE, ACCUMULATEPOINTS, GOODSNUMBER, SOLDNUMBER, GOODSID, CREATETIME, MODIFYTIME, GOODSDESCRIBE, SHOPID, TYPEID, PRICE");
        buf.append(") VALUES (");
        buf.append("    '" + cut(data.getGoodsUID(), 128) + "', '" + cut(data.getGoodsName(), 255) + "', '" + cut(data.getState(), 255) + "', '" + cut(data.getGoodsType(), 255) + "', " + data.getAccumulatePoints() + ", " + data.getGoodsNumber() + ", " + data.getSoldNumber() + ", '" + cut(data.getGoodsId(), 255) + "', " + data.getCreateTime() + ", " + data.getModifyTime() + ", '" + cut(data.getGoodsDescribe(), 255) + "', '" + cut(data.getShopId(), 255) + "', '" + cut(data.getTypeId(), 255) + "', " + data.getPrice() + "");
        buf.append(")");
        return buf.toString();
    }

    @Override
    public String getDeleteRowsStatement(Long rowId) {
        return "DELETE FROM " + getConnectionName() + ".ACCUMULATEGOODSLIST WHERE  RECORD_ID = " + rowId;
    }

    @Override
    public String getLocalUpdateStatement(AccumulateGoodsListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".ACCUMULATEGOODSLIST \n");
        buf.append("   SET ");
        if(data.getGoodsUID() != null) {
            buf.append(sep.toString() + "\n      GOODSUID = '" + cut(data.getGoodsUID(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsName() != null) {
            buf.append(sep.toString() + "\n      GOODSNAME = '" + cut(data.getGoodsName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getState() != null) {
            buf.append(sep.toString() + "\n      STATE = '" + cut(data.getState(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsType() != null) {
            buf.append(sep.toString() + "\n      GOODSTYPE = '" + cut(data.getGoodsType(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getAccumulatePoints() != null) {
            buf.append(sep.toString() + "\n      ACCUMULATEPOINTS = " + data.getAccumulatePoints() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsNumber() != null) {
            buf.append(sep.toString() + "\n      GOODSNUMBER = " + data.getGoodsNumber() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getSoldNumber() != null) {
            buf.append(sep.toString() + "\n      SOLDNUMBER = " + data.getSoldNumber() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsId() != null) {
            buf.append(sep.toString() + "\n      GOODSID = '" + cut(data.getGoodsId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getModifyTime() != null) {
            buf.append(sep.toString() + "\n      MODIFYTIME = " + data.getModifyTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsDescribe() != null) {
            buf.append(sep.toString() + "\n      GOODSDESCRIBE = '" + cut(data.getGoodsDescribe(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getShopId() != null) {
            buf.append(sep.toString() + "\n      SHOPID = '" + cut(data.getShopId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getTypeId() != null) {
            buf.append(sep.toString() + "\n      TYPEID = '" + cut(data.getTypeId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getPrice() != null) {
            buf.append(sep.toString() + "\n      PRICE = " + data.getPrice() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(!hasData) return "";

        hasData = false;
        buf.append(" WHERE TRUE\n");
        if(data.getGoodsUID() != null) {
            buf.append("    AND GOODSUID = '" + data.getGoodsUID() + "'\n");
            hasData = true;
        }
        if(!hasData) return "";

        return buf.toString();
    }

    @Override
    public String getUpdateByIdStatement(AccumulateGoodsListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".ACCUMULATEGOODSLIST \n");
        buf.append("   SET ");
        if(data.getGoodsUID() != null) {
            buf.append(sep.toString() + "\n      GOODSUID = '" + cut(data.getGoodsUID(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsName() != null) {
            buf.append(sep.toString() + "\n      GOODSNAME = '" + cut(data.getGoodsName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getState() != null) {
            buf.append(sep.toString() + "\n      STATE = '" + cut(data.getState(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsType() != null) {
            buf.append(sep.toString() + "\n      GOODSTYPE = '" + cut(data.getGoodsType(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getAccumulatePoints() != null) {
            buf.append(sep.toString() + "\n      ACCUMULATEPOINTS = " + data.getAccumulatePoints() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsNumber() != null) {
            buf.append(sep.toString() + "\n      GOODSNUMBER = " + data.getGoodsNumber() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getSoldNumber() != null) {
            buf.append(sep.toString() + "\n      SOLDNUMBER = " + data.getSoldNumber() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsId() != null) {
            buf.append(sep.toString() + "\n      GOODSID = '" + cut(data.getGoodsId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getModifyTime() != null) {
            buf.append(sep.toString() + "\n      MODIFYTIME = " + data.getModifyTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getGoodsDescribe() != null) {
            buf.append(sep.toString() + "\n      GOODSDESCRIBE = '" + cut(data.getGoodsDescribe(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getShopId() != null) {
            buf.append(sep.toString() + "\n      SHOPID = '" + cut(data.getShopId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(data.getTypeId() != null) {
            buf.append(sep.toString() + "\n      TYPEID = '" + cut(data.getTypeId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getPrice() != null) {
            buf.append(sep.toString() + "\n      PRICE = " + data.getPrice() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if(!hasData) return "";

        hasData = false;
        buf.append(" WHERE RECORD_ID = " + data.getRecordId());
        return buf.toString();
    }

    @Override
    public AccumulateGoodsListBean extractLine(ResultSet resultSet) throws Exception {
        AccumulateGoodsListBean result = new AccumulateGoodsListBean();

        result.setRecordId(resultSet.getLong("RECORD_ID"));
        result.setGoodsUID(UID.recover(resultSet.getString("goodsUID")));
        result.setGoodsName(resultSet.getString("goodsName"));
        result.setState(resultSet.getString("state"));
        result.setGoodsType(resultSet.getString("goodsType"));
        result.setAccumulatePoints(resultSet.getLong("accumulatePoints"));
        result.setGoodsNumber(resultSet.getLong("goodsNumber"));
        result.setSoldNumber(resultSet.getLong("soldNumber"));
        result.setGoodsId(resultSet.getString("goodsId"));
        result.setCreateTime(resultSet.getLong("createTime"));
        result.setModifyTime(resultSet.getLong("modifyTime"));
        result.setGoodsDescribe(resultSet.getString("goodsDescribe"));
        result.setShopId(resultSet.getString("shopId"));
        result.setTypeId(resultSet.getString("typeId"));
        result.setPrice(resultSet.getDouble("price"));
        return result;
    }

    @Override
    public String getWherePart(AccumulateGoodsListCriteria criteria, SortSpec sort) {
        StringBuilder buf = new StringBuilder(" TRUE ");

        criteria.setGoodsNameLike(searchString(criteria.getGoodsNameLike()));
        criteria.setStateLike(searchString(criteria.getStateLike()));
        criteria.setGoodsTypeLike(searchString(criteria.getGoodsTypeLike()));
        criteria.setGoodsIdLike(searchString(criteria.getGoodsIdLike()));
        criteria.setGoodsDescribeLike(searchString(criteria.getGoodsDescribeLike()));
        criteria.setShopIdLike(searchString(criteria.getShopIdLike()));
        criteria.setTypeIdLike(searchString(criteria.getTypeIdLike()));

        if(criteria.getRecordId() != null) {
            buf.append("    AND RECORD_ID = " + criteria.getRecordId());
        }
        if(criteria.getGoodsUIDEquals() != null) {
            buf.append("    AND GOODSUID = '" + criteria.getGoodsUIDEquals() + "'");
        }
        if(criteria.getGoodsUIDNotEquals() != null) {
            buf.append("    AND GOODSUID != '" + criteria.getGoodsUIDNotEquals() + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsNameEquals())) {
            buf.append("    AND GOODSNAME = '" + cut(criteria.getGoodsNameEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsNameNotEquals())) {
            buf.append("    AND GOODSNAME != '" + cut(criteria.getGoodsNameNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsNameLike())) {
            buf.append("    AND GOODSNAME LIKE '" + cut(criteria.getGoodsNameLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getStateEquals())) {
            buf.append("    AND STATE = '" + cut(criteria.getStateEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getStateNotEquals())) {
            buf.append("    AND STATE != '" + cut(criteria.getStateNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getStateLike())) {
            buf.append("    AND STATE LIKE '" + cut(criteria.getStateLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsTypeEquals())) {
            buf.append("    AND GOODSTYPE = '" + cut(criteria.getGoodsTypeEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsTypeNotEquals())) {
            buf.append("    AND GOODSTYPE != '" + cut(criteria.getGoodsTypeNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsTypeLike())) {
            buf.append("    AND GOODSTYPE LIKE '" + cut(criteria.getGoodsTypeLike()) + "'");
        }
        if(criteria.getAccumulatePointsUpper() != null) {
            buf.append("    AND ACCUMULATEPOINTS <= " + criteria.getAccumulatePointsUpper());
        }
        if(criteria.getAccumulatePointsLower() != null) {
            buf.append("    AND ACCUMULATEPOINTS >= " + criteria.getAccumulatePointsLower());
        }
        if(criteria.getAccumulatePointsEquals() != null) {
            buf.append("    AND ACCUMULATEPOINTS = '" + criteria.getAccumulatePointsEquals() + "'");
        }
        if(criteria.getAccumulatePointsNotEquals() != null) {
            buf.append("    AND ACCUMULATEPOINTS != '" + criteria.getAccumulatePointsNotEquals() + "'");
        }
        if(criteria.getGoodsNumberUpper() != null) {
            buf.append("    AND GOODSNUMBER <= " + criteria.getGoodsNumberUpper());
        }
        if(criteria.getGoodsNumberLower() != null) {
            buf.append("    AND GOODSNUMBER >= " + criteria.getGoodsNumberLower());
        }
        if(criteria.getGoodsNumberEquals() != null) {
            buf.append("    AND GOODSNUMBER = '" + criteria.getGoodsNumberEquals() + "'");
        }
        if(criteria.getGoodsNumberNotEquals() != null) {
            buf.append("    AND GOODSNUMBER != '" + criteria.getGoodsNumberNotEquals() + "'");
        }
        if(criteria.getSoldNumberUpper() != null) {
            buf.append("    AND SOLDNUMBER <= " + criteria.getSoldNumberUpper());
        }
        if(criteria.getSoldNumberLower() != null) {
            buf.append("    AND SOLDNUMBER >= " + criteria.getSoldNumberLower());
        }
        if(criteria.getSoldNumberEquals() != null) {
            buf.append("    AND SOLDNUMBER = '" + criteria.getSoldNumberEquals() + "'");
        }
        if(criteria.getSoldNumberNotEquals() != null) {
            buf.append("    AND SOLDNUMBER != '" + criteria.getSoldNumberNotEquals() + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsIdEquals())) {
            buf.append("    AND GOODSID = '" + cut(criteria.getGoodsIdEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsIdNotEquals())) {
            buf.append("    AND GOODSID != '" + cut(criteria.getGoodsIdNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsIdLike())) {
            buf.append("    AND GOODSID LIKE '" + cut(criteria.getGoodsIdLike()) + "'");
        }
        if(criteria.getCreateTimeUpper() != null) {
            buf.append("    AND CREATETIME <= " + criteria.getCreateTimeUpper());
        }
        if(criteria.getCreateTimeLower() != null) {
            buf.append("    AND CREATETIME >= " + criteria.getCreateTimeLower());
        }
        if(criteria.getCreateTimeEquals() != null) {
            buf.append("    AND CREATETIME = '" + criteria.getCreateTimeEquals() + "'");
        }
        if(criteria.getCreateTimeNotEquals() != null) {
            buf.append("    AND CREATETIME != '" + criteria.getCreateTimeNotEquals() + "'");
        }
        if(criteria.getModifyTimeUpper() != null) {
            buf.append("    AND MODIFYTIME <= " + criteria.getModifyTimeUpper());
        }
        if(criteria.getModifyTimeLower() != null) {
            buf.append("    AND MODIFYTIME >= " + criteria.getModifyTimeLower());
        }
        if(criteria.getModifyTimeEquals() != null) {
            buf.append("    AND MODIFYTIME = '" + criteria.getModifyTimeEquals() + "'");
        }
        if(criteria.getModifyTimeNotEquals() != null) {
            buf.append("    AND MODIFYTIME != '" + criteria.getModifyTimeNotEquals() + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsDescribeEquals())) {
            buf.append("    AND GOODSDESCRIBE = '" + cut(criteria.getGoodsDescribeEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsDescribeNotEquals())) {
            buf.append("    AND GOODSDESCRIBE != '" + cut(criteria.getGoodsDescribeNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getGoodsDescribeLike())) {
            buf.append("    AND GOODSDESCRIBE LIKE '" + cut(criteria.getGoodsDescribeLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getShopIdEquals())) {
            buf.append("    AND SHOPID = '" + cut(criteria.getShopIdEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getShopIdNotEquals())) {
            buf.append("    AND SHOPID != '" + cut(criteria.getShopIdNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getShopIdLike())) {
            buf.append("    AND SHOPID LIKE '" + cut(criteria.getShopIdLike()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getTypeIdEquals())) {
            buf.append("    AND TYPEID = '" + cut(criteria.getTypeIdEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getTypeIdNotEquals())) {
            buf.append("    AND TYPEID != '" + cut(criteria.getTypeIdNotEquals()) + "'");
        }
        if(!Strings.isNullOrEmpty(criteria.getTypeIdLike())) {
            buf.append("    AND TYPEID LIKE '" + cut(criteria.getTypeIdLike()) + "'");
        }
        if (criteria.getPriceUpper() != null) {
            buf.append("    AND PRICE <= " + criteria.getPriceUpper());
        }
        if (criteria.getPriceLower() != null) {
            buf.append("    AND PRICE >= " + criteria.getPriceLower());
        }
        if(sort != null && sort.getColumns().size() > 0) {
            buf.append(" ORDER BY ");
            sort.getColumns().forEach(c->buf.append(c.getColumnName().toUpperCase())
                .append(" ").append((c.getAscending() != null && c.getAscending()) ? "ASC" : "DESC")
                .append(", "));
            buf.append("RECORD_ID ASC");
        }
        return buf.toString();
    }

    @Override
    public <T extends Unit > String getUpdateStatement(T source) {
        AccumulateGoodsListBean data = null;

        if(source instanceof com.restaurant.application.indexes.accumulategoodslist.mysql.AccumulateGoodsListFromgoodsUIDIF) {
            data = new AccumulateGoodsListBean();
             ((com.restaurant.application.indexes.accumulategoodslist.mysql.AccumulateGoodsListFromgoodsUIDIF)source).getGoodsUIDDataForAccumulateGoodsList(data);
        }

        return data == null ? null: getLocalUpdateStatement(data);
    }
}
